package org.jet.emall.prod.controller;

import io.swagger.annotations.ApiOperation;
import org.jet.emall.common.constant.SpuBanSaleValue;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.log.AdminOperationLog;
import org.jet.emall.log.LogType;
import org.jet.emall.prod.dto.*;
import org.jet.emall.prod.model.NewSkuModel;
import org.jet.emall.prod.model.NewSpuModel;
import org.jet.emall.prod.model.UpdateSkuDetailsModel;
import org.jet.emall.prod.service.CustomProductService;
import org.jet.emall.prod.transfer.SkuImageVoTransfer;
import org.jet.emall.prod.transfer.SkuVoTransfer;
import org.jet.emall.prod.transfer.SpuImageVoTransfer;
import org.jet.emall.prod.transfer.SpuVoTransfer;
import org.jet.emall.prod.vo.*;
import org.jet.emall.prod.wrapper.SkuIdAndCountWrapper;
import org.jet.emall.prod.wrapper.SkuIdAndNameWrapper;
import org.jet.emall.prod.wrapper.SkuIdAndPriceWrapper;
import org.jet.emall.prod.wrapper.SpuIdsAndBanSaleWrapper;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-15 09:56
 */
@RestController
public class ProductController {
    @Autowired
    private CustomProductService productService;


    @ApiInformation(function = "获取SPU的SKU列表")
    @ApiOperation(value = "获取商品的SKU")
    @RequestMapping(value = "/products/skus/{spuId}",method = RequestMethod.GET)
    public CommonResult<List<SkuVO>> listSkusOfSpu(@PathVariable Long spuId){
        List<SkuDTO> dtoList = productService.listSkusOfSpu(spuId);
        if(dtoList==null || dtoList.isEmpty()){
            return CommonResult.notFound("找不到id为"+spuId+"的SPU的SKU列表");
        }
        List<SkuVO> voList = SkuVoTransfer.INSTANCE.toVoList(dtoList);
        return CommonResult.success(voList,"获取spu的sku列表成功");
    }


    @ApiInformation(function = "分页查找SPU",menu = "商品管理")
    @ApiOperation(value = "分页查找SPU")
    @RequestMapping(value = "/products/spus",method = RequestMethod.GET)
    public CommonResult<CommonPage<SpuViewVO>> spuPage(String query, Integer pageNum, Integer pageSize, String brandQuery, Integer cateId, Byte banSale) {
        CommonPage<SpuViewDTO> page = productService.spuPage(query, pageNum, pageSize, brandQuery, cateId, banSale);
        ((CommonPage<SpuViewVO>) (Object) page).setList(SpuVoTransfer.INSTANCE.toVoList(page.getList()));
        return CommonResult.success((CommonPage<SpuViewVO>) (Object) page,"分页查找SPU成功");
    }

    @ApiInformation(function = "用code查询SPU")
    @ApiOperation(value = "用code查询SPU")
    @RequestMapping(value = "/products/{spuCode}",method = RequestMethod.GET)
    public CommonResult<SpuViewVO> findSpuByCode(@PathVariable String spuCode){
        SpuViewDTO spuDTO = productService.findSpuByCode(spuCode);
        if(spuDTO!=null) {
            SpuViewVO spuViewVO = SpuVoTransfer.INSTANCE.toVo(spuDTO);
            return CommonResult.success(spuViewVO,"查询到code为["+spuCode+"]的SPU成功");
        }else{
            return CommonResult.notFound("未找到code为"+spuCode+"的SPU");
        }
    }

    @ApiInformation(function = "添加SPU",menu = "商品管理")
    @AdminOperationLog(value = "添加SPU",logType = LogType.ADD)
    @ApiOperation(value = "添加SPU")
    @RequestMapping(value = "/products/spu",method = RequestMethod.POST)
    public CommonResult<Object> addSpu(@RequestBody @Valid NewSpuModel spuModel){
        NewSpuDTO newSpuDTO = SpuVoTransfer.INSTANCE.toDto(spuModel);
        productService.saveSpu(newSpuDTO);
        return CommonResult.success(null,"添加商品SPU成功");
    }

    @ApiInformation(function = "上架/下架",menu = "商品管理")
    @AdminOperationLog(value = "上架/下架",logType = LogType.UPDATE)
    @ApiOperation(value = "上架/下架")
    @RequestMapping(value = "/products/spu/banSale",method = RequestMethod.PUT)
    public CommonResult<Object> banSaleSpuList(@RequestBody @Valid SpuIdsAndBanSaleWrapper wrapper){
        //添加对枚举值的校验,没必要去定义一套注解来校验这个枚举值
        if(!SpuBanSaleValue.isValidValue(wrapper.getBanSale())){
            return CommonResult.badRequest("参数错误");
        }
        productService.banSale(wrapper.getSpuIds(),wrapper.getBanSale());
        return CommonResult.success(null,wrapper.getBanSale().equals(SpuBanSaleValue.BAN)?"下架成功":"上架成功");
    }

    @ApiInformation(function = "删除SPU",menu = "商品管理")
    @AdminOperationLog(value = "删除SPU",logType = LogType.DELETE)
    @ApiOperation(value="删除SPU")
    @RequestMapping(value = "/products",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteSpu(@RequestBody @Valid List<Long> idList){
        if(idList==null||idList.isEmpty()){
            return CommonResult.badRequest("参数错误");
        }
        productService.deleteSpu(idList);
        return CommonResult.success(null,"删除SPU成功");
    }

    @ApiInformation(function = "删除SKU",menu = "商品管理")
    @AdminOperationLog(value = "删除SKU",logType = LogType.DELETE)
    @ApiOperation(value ="删除SKU")
    @RequestMapping(value = "/products/sku",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteSku(@RequestBody List<Long> idList){
        if(idList==null||idList.isEmpty()){
            return CommonResult.badRequest("参数错误");
        }
        productService.deleteSku(idList);
        return CommonResult.success(null,"删除SKU成功");
    }

    @ApiInformation(function = "SKU详情",menu = "商品管理")
    @ApiOperation(value = "获取SKU详情")
    @RequestMapping(value = "/products/sku/{id}", method = RequestMethod.GET)
    public CommonResult<SkuDetailsVO> getSKUOverAll(@PathVariable Long id){
        if(id==null){
            return CommonResult.badRequest("参数错误");
        }
        SkuDetailsDTO dto = productService.getSkuDetails(id);
        if(dto==null){
            return CommonResult.notFound("找不到id为"+id+"的SKU详情");
        }
        SkuDetailsVO vo = SkuVoTransfer.INSTANCE.toVo(dto);
        return CommonResult.success(vo,"获取SKU详情成功");
    }

    @ApiInformation(function = "修改SKU",menu = "商品管理")
    @AdminOperationLog(value = "修改SKU",logType = LogType.UPDATE)
    @ApiOperation(value = "修改SKU")
    @RequestMapping(value = "/products/sku",method = RequestMethod.PUT)
    public CommonResult<Object> updateSkuOverAll(@RequestBody UpdateSkuDetailsModel updateSkuDetailsModel){

        SkuDetailsDTO dto = SkuVoTransfer.INSTANCE.toDto(updateSkuDetailsModel);
        productService.updateSkuDetails(dto);
        return CommonResult.success(null,"修改成功");
    }

    @ApiInformation(function = "添加SKU",menu = "商品管理")
    @AdminOperationLog(value = "添加SKU",logType = LogType.ADD)
    @ApiOperation(value = "添加SKU")
    @RequestMapping(value = "/products/sku",method = RequestMethod.POST)
    public CommonResult<Object> addSku(@RequestBody NewSkuModel skuModel){
        NewSkuDTO dto = SkuVoTransfer.INSTANCE.toDto(skuModel);
        productService.saveSku(dto);
        return CommonResult.success(null,"添加SKU成功");
    }

    @ApiInformation(function = "获取SPU图片")
    @ApiOperation(value = "获取SPU图片")
    @RequestMapping( value = "/products/spu/{spuId}/images",method = RequestMethod.GET)
    public CommonResult<List<SpuImageVO>> getImagesOfSpu(@PathVariable Long spuId){
        List<SpuImageDTO> spuImageDTOList = productService.getSpuImages(spuId);
        List<SpuImageVO> voList = SpuImageVoTransfer.INSTANCE.toVoList(spuImageDTOList);
        return CommonResult.success(voList,"获取SPU的图片列表成功");
    }

    @ApiInformation(function = "获取SKU图片")
    @ApiOperation(value = "获取SKU图片")
    @RequestMapping( value = "/products/sku/{skuId}/images",method = RequestMethod.GET)
    public CommonResult<List<SkuImageVO>> getImagesOfSku(@PathVariable Long skuId){
        List<SkuImageDTO> skuImageDTOList = productService.getSkuImages(skuId);
        List<SkuImageVO> voList = SkuImageVoTransfer.INSTANCE.toVoList(skuImageDTOList);
         return CommonResult.success(voList,"获取SKU的图片列表成功");
    }

    @ApiInformation(function = "删除SPU图片",menu = "商品管理")
    @AdminOperationLog(value = "删除SPU图片",logType = LogType.DELETE)
    @ApiOperation(value="删除SPU图片")
    @RequestMapping( value ="/products/spu/image/{id}",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteImageOfSpu(@PathVariable Long id){
        productService.deleteSpuImages(id);
        return CommonResult.success(null,"删除SPU的图片成功");
    }

    @ApiInformation(function = "删除SKU图片",menu = "商品管理")
    @AdminOperationLog(value = "删除SKU图片",logType = LogType.DELETE)
    @ApiOperation(value="删除SKU图片")
    @RequestMapping( value ="/products/sku/image/{id}",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteSkuImage(@PathVariable Long id){
        productService.deleteSkuImage(id);
        return CommonResult.success(null,"删除SKU的图片成功");
    }


    @ApiInformation(function = "添加SPU图片",menu = "商品管理")
    @AdminOperationLog(value = "添加SPU图片",logType = LogType.ADD)
    @ApiOperation(value = "添加SPU图片")
    @RequestMapping( value = "/products/spu/image",method = RequestMethod.POST)
    public CommonResult<Object> addSpuImage(@RequestBody SpuImageVO spuImageVO){
        SpuImageDTO dto = SpuImageVoTransfer.INSTANCE.toDto(spuImageVO);
        productService.saveSpuImage(dto);
        return CommonResult.success(null,"添加SPU图片成功");
    }

    @ApiInformation(function = "添加SKU图片",menu = "商品管理")
    @AdminOperationLog(value = "添加SKU图片",logType = LogType.ADD)
    @ApiOperation(value = "添加SKU图片")
    @RequestMapping( value = "/products/sku/image",method = RequestMethod.POST)
    public CommonResult<Object> addSkuImage(@RequestBody SkuImageVO skuImageVO){
        SkuImageDTO dto = SkuImageVoTransfer.INSTANCE.toDto(skuImageVO);
        productService.saveSkuImage(dto);
        return CommonResult.success(null,"添加SKU图片成功");
    }

    @ApiInformation(function = "修改SKU价格",menu = "商品管理")
    @AdminOperationLog(value = "修改SKU价格",logType = LogType.UPDATE)
    @ApiOperation(value = "修改SKU价格")
    @RequestMapping( value = "/products/sku/price",method = RequestMethod.PATCH)
    public CommonResult<Object> updateSkuPrice(@RequestBody SkuIdAndPriceWrapper wrapper){
        Long skuId = wrapper.getSkuId();
        Long price = wrapper.getPrice();
        if( skuId==null || price==null || price<0){
            return CommonResult.badRequest("参数错误");
        }
        productService.updateSkuPrice(skuId,price);
        return CommonResult.success(null,"设置价格成功");
    }

    @ApiInformation(function = "修改SKU名称",menu = "商品管理")
    @AdminOperationLog(value = "修改SKU名称",logType = LogType.UPDATE)
    @ApiOperation(value = "修改SKU名称")
    @RequestMapping( value = "/products/sku/name",method = RequestMethod.PATCH)
    public CommonResult<Object> updateSkuName(@RequestBody SkuIdAndNameWrapper wrapper){
        Long skuId = wrapper.getSkuId();
        String name = wrapper.getName();
        if( skuId==null || name==null || name.isEmpty()){
            return CommonResult.badRequest("参数错误");
        }
        productService.updateSkuName(skuId,name);
        return CommonResult.success(null,"设置名称成功");
    }

    @ApiInformation(function = "修改SKU库存",menu = "商品管理")
    @AdminOperationLog(value = "修改SKU库存",logType = LogType.UPDATE)
    @ApiOperation(value = "修改SKU库存")
    @RequestMapping( value = "/products/sku/count",method = RequestMethod.PATCH)
    public CommonResult<Object> updateSkuCount(@RequestBody SkuIdAndCountWrapper wrapper){
        Long skuId = wrapper.getSkuId();
        Integer count = wrapper.getCount();
        if(skuId==null || count==null || count<0){
            return CommonResult.badRequest("参数错误");
        }
        productService.updateSkuCount(skuId,count);
        return CommonResult.success(null,"修改库存成功");
    }

}
